<template>
  <div>
    <!-- 面包屑导航区域 -->
    <div class="bread_box">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>文章管理</el-breadcrumb-item>
        <el-breadcrumb-item>文章分类</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与按钮区域 -->
      <el-form
        class="search_from"
        size="small"
        :inline="isinline"
        label-width="70px"
      >
        <el-form-item label="分类名称">
          <el-input placeholder="请输入分类名称" clearable></el-input>
        </el-form-item>

        <el-form-item label="创建时间">
          <el-date-picker
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini">重置</el-button>
        </el-form-item>
      </el-form>

      <div>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="toAddDialog()"
          >新增</el-button
        >
        <el-button type="success" icon="el-icon-edit" size="mini"
          >修改</el-button
        >
        <el-button type="danger" icon="el-icon-delete" size="mini"
          >删除</el-button
        >
        <el-button type="warning" icon="el-icon-download" size="mini"
          >导出</el-button
        >
      </div>

      <!-- 分类列表区域 -->
      <el-table
        :data="ArticleClassList"
        :header-cell-style="{ 'text-align': 'center' }"
        :cell-style="{ 'text-align': 'center' }"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index" label="序号"></el-table-column>
        <!-- <el-table-column label="分类Id" prop="id"></el-table-column> -->
        <el-table-column label="分类名称" prop="className"></el-table-column>
        <el-table-column label="分类描述" prop="className"></el-table-column>
        <el-table-column label="文章数量" prop="num"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 编辑按钮 -->
            <el-button type="text" size="mini" @click="toEditDialog(scope.row)"
              ><i class="el-icon-edit"></i><span>编辑</span></el-button
            >
            <!-- 删除按钮 -->
            <el-button
              type="text"
              size="mini"
              @click="delArticleClass(scope.row)"
              ><i class="el-icon-delete"></i><span>删除</span></el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.cur"
        :page-sizes="[5, 8, 10, 20]"
        :page-size="pageInfo.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>

    <!-- 分类编辑对话框 -->
    <el-dialog
      title="编辑分类"
      :visible.sync="editDialogVisible"
      width="35%"
      @close="editDialogClose()"
    >
      <el-form
        ref="editFormRef"
        :model="editForm"
        laidbel-width="100px"
        class="editFormClass"
      >
        <el-row>
          <el-col :span="18" :offset="3">
            <el-form-item label="分类Id：" prop="id">
              <el-input disabled v-model="editForm.id"></el-input>
            </el-form-item>
            <el-form-item label="分类名称：" prop="name">
              <el-input v-model="editForm.name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editArticleClass()">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 添加分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addDialogVisible"
      width="35%"
      @close="addDialogClose()"
    >
      <el-form
        ref="addFormRef"
        :model="addForm"
        laidbel-width="100px"
        class="editFormClass"
      >
        <el-row>
          <el-col :span="18" :offset="3">
            <el-form-item label="分类名称：" prop="name">
              <el-input v-model="addForm.name"></el-input>
            </el-form-item>
            <el-form-item label="分类描述：" prop="name">
              <el-input v-model="addForm.name"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addArticleClass()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 获取文章分类列表的参数对象
      pageInfo: {
        cur: 1, // 当前页
        size: 5 // 页大小
      },
      // 文章分类列表
      ArticleClassList: [],
      total: 0,
      isinline: true,
      // 隐藏显示编辑对话框
      editDialogVisible: false,
      // 隐藏显示添加对话框
      addDialogVisible: false,
      // 分类编辑表单
      editForm: {
        id: '', // 分类id
        name: '' // 分类名称
      },
      addForm: {
        name: '', // 分类名称
        dec: ''
      }
    }
  },
  methods: {
    // 获取分类列表
    async getArticleClassList () {
      const { data: res } = await this.$http.get('/class/selectPage', { params: this.pageInfo })
      console.log('获取分类列表', res)
      if (res.code !== 200) return this.$message.error('获取文章分类列表数据失败！')
      this.ArticleClassList = res.data.list
      this.total = res.data.total
    },
    // 监听 size 改变的事件
    handleSizeChange (newSize) {
      //   console.log(newSize)
      this.pageInfo.size = newSize
      this.getArticleClassList()
    },
    // 监听 cur 页码值 改变的事件
    handleCurrentChange (newCur) {
      //   console.log(newCur)
      this.pageInfo.cur = newCur
      this.getArticleClassList()
    },
    // 编辑 Dialog 关闭时清空表单
    editDialogClose () {
      this.$refs.editFormRef.resetFields()
    },
    // 添加 Dialog 关闭时清空表单
    addDialogClose () {
      this.$refs.addFormRef.resetFields()
    },
    // 打开添加分类 Dialog
    toAddDialog () {
      this.addDialogVisible = true
    },
    async addArticleClass () {
      var name = { name: this.addForm.name }
      const { data: res } = await this.$http.get('/class/insert', { params: name })
      console.log('添加分类', res)
      if (res.code !== 200) {
        return this.$message.error('添加分类名失败！')
      }
      this.$message.success('添加分类名成功！')
      this.getArticleClassList()
      this.addDialogVisible = false
    },
    // 打开编辑分类 Dialog
    toEditDialog (info) {
      this.editDialogVisible = true
      this.editForm.id = info.id
      this.editForm.name = info.className
    },
    // 编辑分类名称
    async editArticleClass () {
      const { data: res } = await this.$http.get('/class/update', { params: this.editForm })
      console.log('编辑分类', res)
      if (res.code !== 200) {
        return this.$message.error('编辑分类名失败！')
      }
      this.$message.success('编辑分类名成功！')
      this.getArticleClassList()
      this.editDialogVisible = false
    },
    // 删除分类名称
    delArticleClass (info) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        var query = { name: info.className }
        const { data: res } = await this.$http.get('/class/delete', { params: query })
        console.log('删除分类', res)
        if (res.code !== 200) {
          return this.$message.error('删除分类名失败！')
        }
        this.$message.success('删除分类名成功！')
        this.getArticleClassList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  },
  created () {
    this.getArticleClassList()
  }
}
</script>

<style lang="less" scoped>
</style>

<style lang="less">
.editFormClass {
  .el-form-item {
    .el-form-item__label {
      font-weight: 700 !important;
      line-height: 24px !important;
    }
    .el-form-item__content {
      line-height: 24px !important;
    }
  }
}
</style>
